package com.jdew.thkj.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jdew.thkj.R;
import com.jdew.thkj.bean.SearchItemBean;
import com.jdew.thkj.onitemInterface.OnItemClickListener;

import java.text.DecimalFormat;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by yangyang on 2017/9/18.
 */

public class SearchAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private List<SearchItemBean> dataList;
    private LayoutInflater layoutInflater;
    private Context context;
    private View itemView;
    private OnItemClickListener mItemClickListener;

    @Override
    public void onClick(View v) {
        if (mItemClickListener!=null){
            mItemClickListener.onItemClick((Integer) v.getTag());
        }
    }

    public void setItemClickListener(OnItemClickListener itemClickListener) {
        mItemClickListener = itemClickListener;
    }
    public SearchAdapter(Context context, List<SearchItemBean> dataList) {
        this.context=context;
        this.dataList = dataList;
        layoutInflater = LayoutInflater.from(context);
    }
    public void setDataList(List<SearchItemBean> dataList){
        this.dataList=dataList;
        notifyDataSetChanged();
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
         itemView = layoutInflater.inflate(R.layout.product_list_item, parent, false);
        itemView.setOnClickListener(this);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myviewHolder=(MyViewHolder)holder;
        myviewHolder.BindView(holder,position);
        myviewHolder.itemView.setTag(position);
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.product)
        ImageView product;
        @Bind(R.id.name)
        TextView name;
        @Bind(R.id.price)
        TextView price;
        @Bind(R.id.before_price)
        TextView beforePrice;
        @Bind(R.id.quan)
        TextView quan;
        @Bind(R.id.earn)
        TextView earn;
        @Bind(R.id.num)
        TextView num;

        MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
        public void BindView(RecyclerView.ViewHolder holder, int position){
            DecimalFormat df = new DecimalFormat("0.00");
            String youhuiquan = null;
            String manjian = null;
            String showPrice = null;
            String str=dataList.get(position).getCoupon_info();
            if(null!=str){
                youhuiquan=str.substring(str.indexOf("减")+1,str.lastIndexOf("元"));
                manjian=str.substring(1,str.indexOf("元"));
            }else{
                youhuiquan="0";
                manjian="0";
            }
            if(Double.parseDouble(dataList.get(position).getZk_final_price())>=Double.parseDouble(manjian)){
                showPrice=df.format(Double.parseDouble(dataList.get(position).getZk_final_price())-Double.parseDouble(youhuiquan));
            }else{
                showPrice=df.format(Double.parseDouble(dataList.get(position).getZk_final_price()));
            }
            String zhuan = df.format(Double.parseDouble(showPrice)*Double.parseDouble(dataList.get(position).getCommission_rate())/100);
            dataList.get(position).setShowPrice(showPrice);
            ((MyViewHolder) holder).name.setText(dataList.get(position).getTitle());
            ((MyViewHolder) holder).num.setText("月销量:"+dataList.get(position).getVolume());
            ((MyViewHolder) holder).price.setText(showPrice+"");
            ((MyViewHolder) holder).beforePrice.setText(dataList.get(position).getZk_final_price());
            ((MyViewHolder) holder).beforePrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
            ((MyViewHolder) holder).quan.setText(dataList.get(position).getCoupon_info()==null?"0元券":youhuiquan+"元券");
            ((MyViewHolder) holder).earn.setText("赚"+zhuan+"元");
            Glide.with(context).load(dataList.get(position).getPict_url()).into(product);
        }
    }
}
